from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import data_required, length
from sqlalchemy import func
from ..models import db, Post, Tag, tags

def sidebar_data():
    recent = Post.query.order_by(Post.publish_date.desc()).limit(5).all()
    top_tags = db.session.query(
        Tag, func.count(tags.c.post_id).label('total')
    ).join(tags).group_by(Tag).order_by('total DESC').limit(5).all()
    return recent, top_tags

class PostForm(FlaskForm):
    body = TextAreaField("你有什么看法?", validators=[data_required()])
    submit = SubmitField('提交')

class CommentsForm(FlaskForm):
    name = StringField('读者', validators=[data_required(), length(max=255)])
    text = TextAreaField(u'评论', validators=[data_required()])

class Userform(FlaskForm):
    pass